// Copyright Leonid Shlyapnikov 2011
// Distributed under the GNU Lesser General Public License http://www.gnu.org/licenses/lgpl.html
//    author: Leonid Shlyapnikov, created on: Aug 10, 2011

#ifndef FIXPARSER_CPP_SRC_MAIN_FIXPARSER_UINT32TOSTRINGCONVERTER_H_
#define FIXPARSER_CPP_SRC_MAIN_FIXPARSER_UINT32TOSTRINGCONVERTER_H_

#include <boost/utility.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/cstdint.hpp>
#include <boost/integer_traits.hpp>

#include <string>

#include "./log.h"

namespace fixparser {

class Uint32ToStringConverter: boost::noncopyable {
public:
    static const std::string& toString(const uint64_t value) {
        if (value > MAX_VALUE) {
            std::stringstream error;
            error << "Illegal value: " << value << ", Uint32ToStringConverter converts integer values"
                  << " in the interval: [" << MIN_VALUE << ", " << MAX_VALUE << "]";
            BOOST_THROW_EXCEPTION(std::invalid_argument(error.str()));
        }

        static Uint32ToStringConverter instance;
        return instance.uintToStringMap_[value];
    }

    static const boost::uint32_t CAPACITY = boost::integer_traits<uint16_t>::const_max;
    static const boost::uint32_t MIN_VALUE = 0;
    static const boost::uint32_t MAX_VALUE = CAPACITY - 1;

private:
    Uint32ToStringConverter() {
        LOG_DEBUG("initializing UintToStringConverter, MAX_VALUE: " << MAX_VALUE);
        for (boost::uint32_t indx = 0; indx <= MAX_VALUE; indx++) {
            uintToStringMap_[indx] = boost::lexical_cast<std::string>(indx);
        }
    }

    virtual ~Uint32ToStringConverter() {}

    std::string uintToStringMap_[CAPACITY];
};

}

#endif  // FIXPARSER_CPP_SRC_MAIN_FIXPARSER_UINT32TOSTRINGCONVERTER_H_
